<%-- 
    Document   : user-add
    Created on : Feb 9, 2012, 8:24:56 AM
    Author     : PhucAn
--%>

<%@page import="util.SharingUtil"%>
<%@page import="java.util.Date"%>
<%@page import="util.DateTime"%>
<%@page import="util.MD5Encryption"%>
<%@page import="dto.User"%>
<%@page import="dao.UserDAO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link type="text/css" rel="stylesheet" href="../style/calendar.css"/>
        <title>Create user account</title>
        <script type="text/javascript">
            $(document).ready(function(){
                $('#dob').datepicker();
            });
            function submitForm(){
                validate([{name:'username',type:'email'}]);
            }
        </script>
    </head>
    <body>

            <form method="post" action="#">
                <table>
                    <tr>
                        <td>Username: </td>
                        <td><input type="text" id="username" name="username"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Password: </td>
                        <td><input type="password" id="password" name="password"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>First name: </td>
                        <td><input type="text" id="firstname" name="firstname"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Last name: </td>
                        <td><input type="text" id="lastname" name="lastname"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Gender: </td>
                        <td><input type="checkbox" id="gender" name="gender"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Date of birth: </td>
                        <td><input type="text" name="dob" id="dob"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Email: </td>
                        <td><input type="text" id="email" name="email"/></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>Role: </td>
                        <td>
                            <select id="role" name="role">
                                <option value="1">Administrator</option>
                                <option value="2">Teacher</option>
                                <option value="3">Student</option>
                            </select>
                        </td>
                        <td></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td colspan="2">
                            <input type="submit" name="submit" value="Create"/>
                            <input type="reset" name="reset" value="Reset"/>
                        </td>
                    </tr>
                </table>
            </form>
       
    </body>
</html>
<%
    MD5Encryption md5 = new MD5Encryption();
    if (request.getParameter("submit") == null) {
        return;
    }
    UserDAO daoUser = new UserDAO();
    User us = new User();
    us.setUsername(request.getParameter("username"));
    us.setPassword(md5.encrypt(request.getParameter("password")));
    us.setFirstName(request.getParameter("firstname"));
    us.setLastName(request.getParameter("lastname"));
    us.setGender(request.getParameter("gender")!=null);
    us.setDateOfBirth(DateTime.parseToDate(request.getParameter("dob"), DateTime.DATE));
    us.setEmail(request.getParameter("email"));
    us.setRole(Integer.parseInt(request.getParameter("role")));
    us.setRegisteredDate(new Date());
    if (daoUser.create(us) > 0) {
        SharingUtil.showMessage(request, "New user account is created");
    } else {
        SharingUtil.showMessage(request, "User account cannot be created. Action failed.");
    }
%>
